<?
	include_once('header.php');
	include_once('sidebar.php');
	extract($_GET);
	$social_links = array('AIM' => 'aim','MSN' => 'msn','Google Talk' => 'google_talk','Jabber' => 'jabber','Yahoo' => 'yahoo', 'MySpace' => 'myspace','Facebook' => 'facebook', 'Digg' => 'digg', 'Last.FM' => 'lastfm', 'del.icio.us' => 'delicious');
?>

		
		<div id="main">
			
			<h2>User<? if($user) { $user_meta = drizzo_user_info($user); echo ": $user_meta[nice_name]"; } else { echo "s"; } ?></h2>
			<br class="clear"/>
<?
	if($user) { // Begin Single Profile View
		if(drizzo_user_info($user)) { // If the user exists
			extract($user_meta);
?>
			<div id="user_profile">
<?
			if(drizzo_user_info($ID,'avatar')) {
?>
				<img src="<? echo admin_folder . "engine/user_avatar.php?user_id=" . $ID; ?>" alt="<? echo $nice_name; ?> Avatar" width="40" height="40" />
<?
			}
?>
				<h3>User Information</h3>
				<div class="faux_label">Username</div> <div class="faux_input"><? echo $username; ?></div><br class="clear" />
				<div class="faux_label">Role</div> <div class="faux_input"><? if($user_level == 5) { echo "Admin"; } if($user_level == 4) { echo "Editor"; } if($user_level == 3) { echo "Publisher"; } if($user_level == 2) { echo "Poster"; } if($user_level == 1) { echo "Regular"; } if($user_level == '0') { echo "Visitor"; } ?></div><br class="clear" />
				<div class="faux_label">Real Name</div> <div class="faux_input"><? echo $first_name . " " . $last_name; ?></div><br class="clear" />
				<div class="faux_label">Display Name</div> <div class="faux_input"><? echo $nice_name; ?></div><br class="clear" />
				<div class="faux_label">Email</div> <div class="faux_input"><a href="mailto:<? echo $email; ?>" title="Email <? echo $nice_name; ?>"><? echo $email; ?></a></div><br class="clear" />
				<div class="faux_label">URL</div> <div class="faux_input"><a href="<? echo $url; ?>" title="<? echo $url; ?>"><? echo $url; ?></a></div><br class="clear" />
				<div class="faux_label">Gender</div> <div class="faux_input"><? echo $gender; ?></div><br class="clear" />
<?
			if($date_added) {
?>
				<div class="faux_label">Added</div> <div class="faux_input"><? echo date(drizzo_global_options('time_format'), $date_added); ?></div><br class="clear" />
<?
			}
			if($date_edited) {
?>
				<div class="faux_label">Last Edited</div> <div class="faux_input"><? echo date(drizzo_global_options('time_format'),$dated_edited) . " by " . drizzo_user_info($edited_by,'nice_name'); ?></div><br class="clear" />
<?	
	}
?>
				<div class="faux_label">About</div> <div class="faux_input"><? echo drizzo_texturize($about); ?></div><br class="clear" />

				<hr class="rule" />
				
				<h3>Social Links</h3>
<?
			foreach($social_links as $title => $entry) {
				if(preg_match('/http:\/\//',$user_meta[$entry]) || preg_match('/www\./',$user_meta[$entry])) {
					$link = true;
					if(!preg_match('/http:\/\//',$user_meta[$entry])) {
						$user_meta[$entry] = "http://" . $user_meta[$entry];
					}
				} else {
					$link = false;
				}
				if(strlen($user_meta[$entry]) > 0) {
?>
				<div class="faux_label"><? echo $title; ?></div> <div class="faux_input"><? if($link) { echo "<a href=\"$user_meta[$entry]\" title=\"$title\">$user_meta[$entry]</a>"; } else { echo $user_meta[$entry]; } ?></div><br class="clear" />
<?
				}
			}
			if(drizzo_user_is_allowed('users-edit')) {
?>
				<form>
					<p class="right"><input type="button" class="submit" value="Edit this User" onclick="window.location = 'users.php?edit=<? echo $ID; ?>'" /></p>
				</form>
<?
			}
?>
			</div>
<?
		} else { // If the user does not exist
?>
			<p>The user could not be found</p>
<?
		}
	} elseif($edit) { // End Single Profile View; Begin Edit Profile
		if($_POST) {
			echo drizzo_user_update();
		}
		$user_meta = drizzo_user_info($edit);
		if(($user_meta && drizzo_user_is_allowed('users-edit')) || $edit == session_user_id) {
?>
			<div id="user_profile">
				<form name="user-edit" id="user-edit" action="" method="post" enctype="multipart/form-data">
					<div class="faux_label">Username</div> <div class="faux_input"><? echo $user_meta['username']; ?> (Cannot be changed)</div><br class="clear" />
					<label for="new_password">New Password</label> <input type="password" name="new_password" /><br class="clear" />
					<label for="new_password2">Confirm New Password</label> <input type="password" name="new_password2" /><br class="clear" />
					<label for="nice_name">Nickname</label> <input type="text" name="nice_name" value="<? echo $user_meta['nice_name']; ?>" id="nice_name" /><br class="clear" />
					<label for="display_name">Display Name</label> <select name="display_name"><option value="nicename"><? echo $user_meta['nice_name']; ?></option><option value="firstname"><? echo $user_meta['first_name']; ?></option><option value="firstname_lastname"><? echo $user_meta['first_name'] . " " . $user_meta['last_name']; ?></option><option value="lastname_firstname"><? echo $user_meta['last_name'] . ", " . $user_meta['first_name']; ?></option><option value="username"><? echo $user_meta['username']; ?></option></select><br class="clear" />
<?
			$exclusions = array('avatar','username','user_level','date_added','date_edited','edited_by','username_md5','password','password_md5','about','gender','ID');
			$titles = array_merge(array_keys($social_links),array('First Name','Last Name','URL','Email'));
			$names = array_merge(array_values($social_links),array('first_name','last_name','url','email'));
			foreach($user_meta as $name => $value) {
				if(!array_search($name,$exclusions) && preg_match('/[^0-9]/',$name)) {
?>
					<label for="<? echo $name ?>"><? echo str_replace($names,$titles,$name); ?></label> <input type="text" name="<? echo $name; ?>" value="<? echo $value; ?>" /><br class="clear" />
<?
				}
			}
?>
					<input type="hidden" name="MAX_FILE_SIZE" value="83886080" style="display: none" />
					<label for="avatar">Avatar</label> <? if(drizzo_user_info($user_meta['ID'],'avatar')) { ?><img src="<? echo admin_folder . "engine/user_avatar.php?user_id=" . $user_meta['ID']; ?>" alt="<? echo $nice_name; ?> Avatar" width="40" height="40" /><? } ?><br class="clear" />
					<label for="new_avatar">New Avatar</label> <input type="file" name="avatar" /><br class="clear" />
<? 
			if(drizzo_user_is_allowed('users-promote')) { 
?>
					<label for="user_level">Role</label><select name="user_level"><? if(session_user_level == 5) { ?><option value="5"<? if($user_meta['user_level'] == 5) { echo " selected"; } ?>>Admin</option><? } ?><option value="4"<? if($user_meta['user_level'] == 4) { echo " selected"; } ?>>Editor</option><option value="3"<? if($user_meta['user_level'] == 3) { echo " selected"; } ?>>Publisher</option><option value="2"<? if($user_meta['user_level'] == 2) { echo " selected"; } ?>>Poster</option><option value="1"<? if($user_meta['user_level'] == 1) { echo " selected"; } ?>>Regular</option><option value="0"<? if($user_meta['user_level'] == "0" || !$user_meta['user_level']) { echo " selected"; } ?>>Visitor</option></select><br class="clear" />
<?
			}
?>
					<label for="gender">Gender</label> <select name="gender"><option value=""<? if(!$user_meta['gender']) { echo " selected"; } ?>></option><option value="female"<? if($user_meta['gender'] == 'female') { echo " selected"; }?>>Female</option><option value="male"<? if($user_meta['gender'] == 'male') { echo " selected"; }?>>Male</option></select><br class="clear" />
					<label for="about">About</label> <textarea name="about"><? echo $user_meta['about']; ?></textarea><br class="clear" />
					<p class="center"><input type="button" onclick="history.go(-1)" value="Cancel" class="submit" /><input type="submit" value="Save Changes" class="submit" /></p>
				</form>
				
				<br class="clear" />
			</div>
<?
		} else {
			echo "<p class=\"alert\">This user cannot be edited</p>";
		}
	} elseif($delete) { // End Edit Profile; Begin Delete User

	} elseif($action == 'add_user') { // End Delete User; Begin Add New User
		if(drizzo_user_is_allowed('users-add')) {
			if(!$_POST) {
?>
			<div id="user_profile">
				<form name="user-add" id="user-add" action="" method="post" enctype="multipart/form-data">
					<label for="username">Username</label> <input type="text" name="username" /><br class="clear" />
					<label for="new_password">Password</label> <input type="password" name="new_password" /><br class="clear" />
					<label for="new_password2">Confirm Password</label> <input type="password" name="new_password2" /><br class="clear" />
					<input type="hidden" name="MAX_FILE_SIZE" value="83886080" style="display: none" />
					<label for="avatar">Avatar (jpg, gif, or png)</label> <input type="file" name="avatar" /><br class="clear" />
<? 
			if(drizzo_user_is_allowed('users-promote')) { 
?>
					<label for="user_level">Role</label><select name="user_level"><option value="5">Admin</option><option value="4">Editor</option><option value="3">Publisher</option><option value="2">Poster</option><option value="1">Regular</option><option value="0" selected>Visitor</option></select><br class="clear" />
<?
			} else {
?>
					<input type="hidden" name="user_level" value="0" />
<?
			}
?>
					<label for="gender">Gender</label> <select name="gender"><option value=""></option><option value="female">Female</option><option value="male">Male</option></select><br class="clear" />
<?
			$titles = array_merge(array_keys($social_links),array('First Name','Last Name','URL','Email','Display Name'));
			$names = array_merge(array_values($social_links),array('first_name','last_name','url','email','nice_name'));
			$user_meta = array();
			while($i < count($titles)) {
				$user_meta[$titles[$i]] = $names[$i];
				$i++;
			}
			foreach($user_meta as $name => $value) {
				if($name != null || $name != '') {
?>
					<label for="<? echo $value ?>"><? echo str_replace($names,$titles,$name); ?></label> <input type="text" name="<? echo $value; ?>" /><br class="clear" />
<?
				}
			}
?>
					<label for="about">About</label> <textarea name="about"></textarea><br class="clear" />
					<p class="center"><input type="button" onclick="history.go(-1)" value="Cancel" class="submit" /><input type="submit" value="Save Changes" class="submit" /></p>
				</form>
				
				<br class="clear" />
			</div>
<?
			} else {
				echo drizzo_user_update();
			}
		} else {
?>
			<p>Sorry, you are not allowed to add a user.</p>
<?
		}
	} else { // End Add User; Begin Default View
		$batch_response = drizzo_users_batch_update();
		if($batch_response) {
			echo $batch_response;
		}
?>
			<form id="users_batch" action="" method="post" id="users_batch" onsubmit="return verify()">
				<div id="table_wrap">
				<table>
					<tr id="first">
						<th>Role</th><th>Username</th><th>Display Name</th><th>Name</th><th>Email</th><th>Website</th><th class="center narrow">Posts</th><th class="center" colspan="3">Action</th><? if(drizzo_user_is_allowed('users-promote') || drizzo_user_is_allowed('users-delete')) { ?><th class="center narrow">Batch</th><? } ?>
					</tr>

<?
		$levels = array(5,4,3,2,1,'0');
		foreach($levels as $level) {
			$users = drizzo_get_the_users($level);
			if($users) {
				foreach($users as $user) {
					extract($user);
?>
					<tr<? if ($alt_count%2) { echo " class=\"alt\""; } ?>>
						<td><? if($prev_user_level != $user_level) { ?><strong><? if($user_level == 5) { echo "Admins"; } if($user_level == 4) { echo "Editors"; } if($user_level == 3) { echo "Publishers"; } if($user_level == 2) { echo "Posters"; } if($user_level == 1) { echo "Regulars"; } if($user_level == '0') { echo "Visitors"; } ?></strong><? } ?></td><td><? echo $username; ?></td><td><? echo $nice_name; ?></td><td><? echo $first_name . " " . $last_name; ?></td><td><? if($email) { ?><a href="mailto:<? echo $email; ?>" title="Email this User"><? echo $email; ?></a><? } ?></td><td><? if($url) { ?><a href="<? echo $url; ?>" title="<? echo $url; ?>"><? echo $url; ?></a><? } ?></td><td class="center"><? echo drizzo_user_posts($ID); ?></td><td class="center narrow"><a href="users.php?user=<? echo $ID; ?>" title="View Profile" class="view_link"><span>View</span></a></td><td class="center narrow"><? if(drizzo_user_is_allowed('users-edit') || $ID == session_user_id) { ?><a href="users.php?edit=<? echo $ID; ?>" title="Edit User" class="edit_link"><span>Edit</span></a><? } else { ?><a href="" title="You are not allowed to edit Users" class="edit_link_off"><span>Edit</span></a><? } ?></td><td class="center narrow"><? if(drizzo_user_is_allowed('users-delete')) { ?><a href="users.php?delete=<? echo $ID; ?>" title="Delete User" class="delete_link"><span>Delete</span></a><? } else { ?><a href="" title="You are not allowed to delete Users" class="delete_link_off"><span>Delete</span></a><? } ?></td><? if(drizzo_user_is_allowed('users-promote') || drizzo_user_is_allowed('users-delete')) { ?><td class="center"><input type="checkbox" class="checkbox" name="batch[<? echo $ID; ?>]" /></td><? } ?>
					</tr>
<?
					$alt_count++;
					$prev_user_level = $user_level;
				}
?>
<?
			}
		}
?>
				</table>
				</div>
				<p>&nbsp;</p>
<?
	if(drizzo_user_is_allowed('users-delete') || drizzo_user_is_allowed('users-promote')) {
?>
				<p class="right"><label for="new_user_level" style="width: auto; float: none; display: inline;  padding: 0 0 1em 0;">Batch Action</label> <select name="batch_action"><option value="" selected>Please Select an Action</option><? if(drizzo_user_is_allowed('users-promote')) { ?><option value="5">Change to Admin</option><option value="4">Change to Editor</option><option value="3">Change to Publisher</option><option value="2">Change to Poster</option><option value="1">Change to Regular</option><option value="0">Change to Visitor</option><? } if(drizzo_user_is_allowed('users-delete')) { ?><option value="delete">Delete Users</option><? } ?></select><br class="clear" />
				<input type="submit" class="submit" value="Update Users" /><br class="clear" /></p>
<?
	}
?>
			</form>

<script type="text/javascript">
	function verify(){
	    if(document.forms['users_batch'].batch_action.value!=""){
	        input_box = confirm("Are you sure you want to perform the batch action on the selected users?");
			if(input_box) {
				return true;
			} else {
				return false;
			}
		}
	}
</script>

<?
	} // End Default View
?>

			<br class="clear" />
			
		</div>
<?
	include_once('footer.php');
?>
